﻿using System.Collections;
using Com.Enemy.EnemySystem.EnemyAI;
using Com.Enemy.EnemySystem.EnemyLifecycle;
using Pathfinding;
using Script.EnemySystem.EnemyState;
using UnityEngine;

namespace Script.Com.Enemy.EnemySystem.EnemyAI
{
    /// <summary>
    /// 卫兵个体AI
    /// </summary>
    public class LancerStateState : EnemyStateBase
    {

        private bool waitChaseAttack;
        private bool chaseAttacking;
        private static readonly int Vertical = Animator.StringToHash("Vertical");

        protected override void Awake()
        {
            base.Awake();
        }

        protected override void Start()
        {
            base.Start();
            
            animTime.AddStateTime(EnemyState.NormalAttack, 5f/20);
            animTime.AddStateTime(EnemyState.ChaseAttack, 5f/9);
            animTime.AddStateTime(EnemyState.Hurt, .5f);
            
            Body.Data.StateColdDown[(int)EnemyState.NormalAttack] = new BaseData(1);
            Body.Data.StateColdDown[(int)EnemyState.ChaseAttack] = new BaseData(0);

            Body.Data.State = EnemyState.Idle;
        }

        protected override void GetIntoState(EnemyState state)
        {
            if(chaseAttacking)
                return;
            
            if (state == EnemyState.NormalAttack)
            {
                waitChaseAttack = true;
            }
            
            base.GetIntoState(state);

        }

        /// <summary>
        /// 退出状态 （注：如果上一次攻击为普击，那么如果追击冷却完毕将发动追击）
        /// </summary>
        protected override void ExitState()
        {
            base.ExitState();

            if (waitChaseAttack)
            {
                if (Body.CoolDownSystem.HadColdDown(EnemyState.ChaseAttack))
                {
                    GetIntoState(EnemyState.ChaseAttack);
                    if(transform.position.y - Body.Target.position.y < 0)
                    {
                        animator.SetInteger(Vertical, 1);
                    }
                    else
                    {
                        animator.SetInteger(Vertical, -1);
                    }
                    chaseAttacking = true;
                    StartCoroutine(EndChasingAttack());
                }
                
                waitChaseAttack = false;
            }

        }
        
        IEnumerator EndChasingAttack()
        {
            yield return new WaitForSeconds(animTime.GetStateTime(EnemyState.ChaseAttack));
            ExitState();
            chaseAttacking = false;
        }

        public void InitChaseVar()
        {
            waitChaseAttack = false;
            chaseAttacking = false;
        }

        public override void TakeDamage(int playerId, float damage, float knockback)
        {
            
            Task task = new Task(TakeDamageTask(playerId, new BaseData(damage), knockback));
            task.Finished += delegate(bool manual)
            {
                ExitState();
            };
            
        }

        protected override IEnumerator TakeDamageTask(int playerId, BaseData damage, float knockback)
        {
            GetIntoState(EnemyState.Hurt);
            Body.TakeDamage(playerId, damage, knockback);
            StartCoroutine(Body.HurtReaction(knockback));
            yield return new WaitForSeconds(animTime.GetStateTime(EnemyState.Hurt));
        }

    }
}
